﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharpSoft.Maths
{
    /// <summary>
    /// 布尔值时变系统过滤器
    /// </summary>
    public class BooleanFilter : TimeVaryingFilter<bool>
    {
        public override void Reset()
        {
            curValue = false;
            changeWeight = 0;
        }

        private int changeWeight = 0;//变化权重
        private bool curValue = false;
        /// <summary>
        /// 权重阈值
        /// </summary>
        public int ThresholdWeight { get; set; } = 3;
        public override bool Update(bool value, double time)
        {
            if (value != curValue)
            {
                changeWeight++;
                if (changeWeight >= this.ThresholdWeight)
                {
                    changeWeight = 0;
                    curValue = value;
                }
            }
            else
            {
                if (changeWeight > 0)
                {
                    changeWeight--;
                }
            }
            return curValue;
        }
    }
}
